package com.jf3q.sup.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.sup.domain.Suppliers;
import com.jf3q.sup.dto.SupplierPageDto;
import com.jf3q.sup.service.CustomersService;
import com.jf3q.sup.service.SuppliersService;
import com.jf3q.sup.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author : 八九宸烨
 * @date : bug退!退!退!
 */

@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    SuppliersService suppliersService;

    @PreAuthorize("hasAuthority('/supplier')")
    @PostMapping("/getSupplierPage")
    public ResultVo getSupplierPage(@RequestBody SupplierPageDto pageDto){
        Page<Suppliers> page= null;
        try {
            page = suppliersService.getPage(pageDto);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("",page);
    }

    @PreAuthorize("hasAuthority('/supplier/add')")
    @PostMapping("/addSupplier")
    public ResultVo addSupplier(@RequestBody Suppliers suppliers){
        try {
            suppliersService.addSupplier(suppliers);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("",null);
    }
    @PreAuthorize("hasAuthority('/supplier/edit')")
    @PostMapping("/updateSupplier")
    public ResultVo updateSupplier(@RequestBody Suppliers suppliers){
        try {
            suppliersService.updateSupplier(suppliers);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("", null);
    }

    @PreAuthorize("hasAuthority('/supplier/delete')")
    @PostMapping("/delSupplier/{sId}")
    public ResultVo delSupplier(@PathVariable Integer sId){
        try {
            suppliersService.removeById(sId);
        } catch (Exception e) {
            throw new RuntimeException("此信息数据与其他数据相连，请先删除与其他关联的数据再进行操作！");
        }
        return ResultVo.success("", null);
    }

    @PreAuthorize("hasAuthority('/supplier/delete')")
    @PostMapping("/delSuppliers/{sIds}")
    public ResultVo delSuppliers(@PathVariable Integer[] sIds){
        try {
            for (Integer sId : sIds) {
                suppliersService.removeById(sId);
            }
        } catch (Exception e) {
            throw new RuntimeException("此信息数据与其他数据相连，请先删除与其他关联的数据再进行操作！");
        }
        return ResultVo.success("",null);
    }
    @GetMapping("/getSupplierAll")
    public ResultVo getSupplier(){
        try {
            List<Suppliers> list = suppliersService.list();
            return ResultVo.success("下拉列表",list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
